var webroot = null;
(function($) {
	$(function() {		
		/**
		 * 截取字符串
		 */
		webroot = $("#webRoot").val() || "";
		console.log($("a.oauth_url").size());
		$("a.oauth_url").click(function(){	
	    	 var uri  = UrlEncode($(this).attr("href"));
			 var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe7a3b3e31937fce3&redirect_uri="+uri+"&response_type=code&scope=snsapi_base&state=123#wechat_redirect";			 
			 window.location.href=url;
			 return false;
		});
	});
	/**
	 * 截取字符串
	 * @return {[type]} [description]
	 */
	function nodeSubStr(_box) {
		var $p = $(_box || document);
		var pattern = /substr\[(\d+)\]/;
		$("[class*='substr']", $p).each(function() {
			var $self = $(this);
			var classStr = $self.attr('class');
			var array = pattern.exec(classStr);
			//如果数组存在
			if (array && array.length >= 2) {
				if ($self.is(":input")) {
					var text = $.trim($(this).val());
					var size = parseInt(array[1]);
					$(this).val(subString(text, size, true));
				} else {
					var text = $.trim($(this).text());
					var size = parseInt(array[1]);
					$(this).text(subString(text, size, true));
				}
			}
		});
	}
})(jQuery);
function subString(str, len, hasDot){
    var newLength = 0;
    var newStr = "";
    var chineseRegex = /[^\x00-\xff]/g;
    var singleChar = "";
    var strLength = str.length;  
    for(var i = 0;i < strLength;i++){
        singleChar = str.charAt(i).toString();
        newLength++;
        if(newLength > len){
            break;
        }
        newStr += singleChar;
    }
    if(hasDot && strLength > len){
        newStr += "...";
    }
    return newStr;
}

function str2asc(strstr){
    return ("0"+strstr.charCodeAt(0).toString(16)).slice(-2);
}
function asc2str(ascasc){
    return String.fromCharCode(ascasc);
} 
function UrlEncode(str){
  var ret="";     
  var strSpecial="!\"#$%&'()*+,/:;<=>?[]^`{|}~%";     
  var tt= "";    

  for(var i=0;i<str.length;i++){     
   var chr = str.charAt(i);     
    var c=str2asc(chr);     
    tt += chr+":"+c+"n";     
    if(parseInt("0x"+c) > 0x7f){     
      ret+="%"+c.slice(0,2)+"%"+c.slice(-2);     
    }else{     
      if(chr==" ")     
        ret+="+";     
      else if(strSpecial.indexOf(chr)!=-1)     
        ret+="%"+c.toString(16);     
      else     
        ret+=chr;     
    }     
  }     
  return ret; 
}
//UrlDecode
function UrlDecode(str){
	  var ret="";
	  for(var i=0;i<str.length;i++){
	   var chr = str.charAt(i);
	    if(chr == "+"){
	      ret+=" ";
	    }else if(chr=="%"){
	     var asc = str.substring(i+1,i+3);
	     if(parseInt("0x"+asc)>0x7f){
	      ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6)));
	      i+=5;
	     }else{
	      ret+=asc2str(parseInt("0x"+asc));
	      i+=2;
	     }
	    }else{
	      ret+= chr;
	    }
	  }
	  return ret;
}